package com.letv.whatslive.web.util.displaycontrol;

import com.google.common.collect.Maps;
import com.letv.whatslive.common.annotation.AutowiredInterface;
import com.letv.whatslive.model.utils.TransferField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by shenyue1 on 2016/5/25.
 */
@Component
public class DisplayControl{
    @Autowired
    private List<AutowiredInterface> autowiredInterfaceList;

    private Map<Class,DisplayObjectInOrder> displayObjectInOrderMap;

    @PostConstruct
    public void init() {
        if(displayObjectInOrderMap==null){
            displayObjectInOrderMap= Maps.newHashMap();
            if(autowiredInterfaceList!=null&&!autowiredInterfaceList.isEmpty()){
                for(int i=0;i<autowiredInterfaceList.size();i++){
                    Class viewClass=autowiredInterfaceList.get(0).getClass();
                    displayObjectInOrderMap.put(viewClass,new DisplayObjectInOrderImpl(viewClass));
                }
            }
        }
    }

    public Boolean isSurport(Object o){
        if(o==null)
            return false;
        Class c=o.getClass();
        Set<Class> classSet=displayObjectInOrderMap.keySet();
        for(Class c1:classSet){
            if(c1.isAssignableFrom(c))
                return true;
        }
        return false;
    }

    public Boolean isSurport(Class c){
        if(c==null)
            return false;
        Set<Class> classSet=displayObjectInOrderMap.keySet();
        for(Class c1:classSet){
            if(c1.isAssignableFrom(c))
                return true;
        }
        return false;
    }

    public List<String> displayFiledNames(Class c) {
        if(!isSurport(c))
            return null;
        return displayObjectInOrderMap.get(c).displayFiledNames();
    }

    public List<String> displayFileds(Object o, TransferField<String> transferField) {
        if(!isSurport(o))
            return null;
        Class c=o.getClass();
        return displayObjectInOrderMap.get(c).displayFileds(o,transferField);
    }
}
